package com.project_test.kun.travelling_1st;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.project_test.kun.travelling_1st.Discovery.Discovery_Fragment;
import com.project_test.kun.travelling_1st.HomePage.HomePageFragment;
import com.project_test.kun.travelling_1st.MinePage.PersonalFragment;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioButton mRadioButton0;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private TextView mToolText;
    private RadioGroup mRadioGroup;
    private FragmentManager mManager;
    private Discovery_Fragment mDiscovery_fragment;
    private HomePageFragment mHomePageFragment;
    private PersonalFragment mPersonalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        getSupportActionBar().hide();
        initView();//初始化布局
        initFragment();//初始化主内容Fragment
        LoadSplash();
    }

    private void initView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.main_radioGroup);
        mToolText = (TextView) findViewById(R.id.toolText);
        mRadioButton0 = (RadioButton) mRadioGroup.getChildAt(0);
        mRadioButton1 = (RadioButton) mRadioGroup.getChildAt(1);
        mRadioButton2 = (RadioButton) mRadioGroup.getChildAt(2);
        BitmpSetSize(); //设置button中图片的大小

        mRadioButton0.setChecked(true);//默认第一个被选中
        //对RadioGroup进行点击监听
        mRadioGroup.setOnCheckedChangeListener(this);

    }


    private void initFragment() {
        mManager = getSupportFragmentManager();
        FragmentTransaction beginTransaction = mManager.beginTransaction();//获得事务
        //首页界面
        mDiscovery_fragment = new Discovery_Fragment();
        beginTransaction.add(R.id.main_frame, mDiscovery_fragment);
        //发现界面
        mHomePageFragment = new HomePageFragment();

        beginTransaction.add(R.id.main_frame, mHomePageFragment);
//        //我的界面
        mPersonalFragment = new PersonalFragment();
        beginTransaction.add(R.id.main_frame, mPersonalFragment);
//        //默认第一个显示
        beginTransaction.show(mHomePageFragment);
        beginTransaction.hide(mDiscovery_fragment);
        beginTransaction.hide(mPersonalFragment);
        beginTransaction.commit();//开启事务

    }


    //    设置button中图片的大小
    private void BitmpSetSize() {
        //设置button中图片的大小
        Drawable drawable = getResources().getDrawable(R.drawable.main_radiobtn0);
        drawable.setBounds(0, 0, 50, 50);
        mRadioButton0.setCompoundDrawables(null, drawable, null, null);

        Drawable drawable1 = getResources().getDrawable(R.drawable.main_radiobtn1);
        drawable1.setBounds(0, 0, 50, 50);
        mRadioButton1.setCompoundDrawables(null, drawable1, null, null);

        Drawable drawable2 = getResources().getDrawable(R.drawable.main_radiobtn2);
        drawable2.setBounds(0, 0, 50, 50);
        mRadioButton2.setCompoundDrawables(null, drawable2, null, null);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.main_radioBtn0:
                FragmentTransaction beginTransaction = mManager.beginTransaction();
                beginTransaction.show(mHomePageFragment);
                beginTransaction.hide(mDiscovery_fragment);
                beginTransaction.hide(mPersonalFragment);
                beginTransaction.commit();
                mToolText.setText("第一出行");
                break;
            case R.id.main_radioBtn1:
                FragmentTransaction beginTransaction1 = mManager.beginTransaction();
                beginTransaction1.show(mDiscovery_fragment);
                beginTransaction1.hide(mHomePageFragment);
                beginTransaction1.hide(mPersonalFragment);
                beginTransaction1.commit();

                mToolText.setText("发现");
                break;
            case R.id.main_radioBtn2:
                FragmentTransaction beginTransaction2 = mManager.beginTransaction();
                beginTransaction2.show(mPersonalFragment);
                beginTransaction2.hide(mHomePageFragment);
                beginTransaction2.hide(mDiscovery_fragment);
                beginTransaction2.commit();
                mToolText.setText("我的");
                break;
        }
    }


    private void LoadSplash() {
        SharedPreferences sharedPreferences = getSharedPreferences("isOne",
                MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("is", true);
        editor.commit();
    }
}
